package com.ftar.urgent.ui;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ftar.urgent.R;
import com.ftar.urgent.constant.ArouterConstant;
import com.ftar.urgent.databinding.ActivityCatalogThirdBinding;
import com.ftar.urgent.viewmodel.CommonEvent;
import com.ftar.urgent.viewmodel.SP_Constants;
import com.ftar.urgent.voice.OffLineVoiceCmd;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author: wei.jin
 * @date: 2023/7/4
 * @email: wei.jin@fanttec.com
 * @description：
 */
@Route(path = ArouterConstant.CATALOG_THIRD_ACTIVITY)
public class CatalogThirdActivity extends BaseActivity {

    private ActivityCatalogThirdBinding mBinding;

    @Autowired
    public int currentId;
    @Autowired
    public int currentCode;
    @Autowired
    public int parentId;
    @Autowired
    public int parentCode;

    private int activeItemIndex = 1;

    @Override
    protected ViewDataBinding getChildBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_catalog_third, null, false);
        mBinding.setHandler(this);
        return mBinding;
    }

    @Override
    protected String voiceHint() {
        return "打开第1/2/3个";
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        init();
    }

    private void init() {
        setActiveItem(activeItemIndex);
        mRootBind.backDesktop.setVisibility(View.VISIBLE);
        mRootBind.clickBackBtn.setVisibility(View.VISIBLE);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                if (activeItemIndex > 1) {
                    activeItemIndex -= 1;
                }
                setActiveItem(activeItemIndex);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (activeItemIndex < 3) {
                    activeItemIndex += 1;
                }
                setActiveItem(activeItemIndex);
                break;
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_F9:
                setItemSelected(activeItemIndex);
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void onAsrCommand(String command) {
        mBinding.thirdName.setTextColor(this.getColor(R.color.white));
        if (!isActivityVisible()) {
            return;
        }
        int pos = 1;
        switch (command) {
            case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_1:
                pos = 1;
                break;
            case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_2:
                pos = 2;
                break;
            case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_3:
                pos = 3;
                break;
            case OffLineVoiceCmd.OfflineVoice.GLOBAL_BACK:
                // 返回
                backPage();
                return;
            case OffLineVoiceCmd.OfflineVoice.GLOBAL_BACK_DESKTOP:
                backDeskTop();
                return;
        }
        setItemSelected(pos);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommonEvent(CommonEvent event) {
        if(!event.getmClass().equals(this.getClass().getName())){
            return;
        }
        switch (event.getMsg()) {
            case SP_Constants.EVENT_COMMAND_ASR:
                String command = event.getData();
                onAsrCommand(command);
                break;
            default:
                break;
        }
    }

    private void setActiveItem(int pos){
        mBinding.thirdName.setTextColor(this.getColor(R.color.white));
        mBinding.thirdName2.setTextColor(this.getColor(R.color.white));
        mBinding.thirdName3.setTextColor(this.getColor(R.color.white));
        switch (pos) {
            case 1:
                mBinding.thirdName.setTextColor(this.getColor(R.color.orange));
                break;
            case 2:
                mBinding.thirdName2.setTextColor(this.getColor(R.color.orange));
                break;
            case 3:
                mBinding.thirdName3.setTextColor(this.getColor(R.color.orange));
                break;
        }

    }

    private void setItemSelected(int pos) {
        setActiveItem(pos);
        switch (pos) {
            case 1:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ARouter.getInstance()
                                .build(ArouterConstant.CAPTURE_ACTIVITY)
                                .withInt("currentId", currentId)
                                .withInt("currentCode", currentCode)
                                .withInt("parentId", parentId)
                                .withInt("parentCode", parentCode)
                                .withInt("pageType", 1)
                                .navigation();
                    }
                }, 500);
                break;

            case 2:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ARouter.getInstance()
                                .build(ArouterConstant.CAPTURE_ACTIVITY)
                                .withInt("currentId", currentId)
                                .withInt("currentCode", currentCode)
                                .withInt("parentId", parentId)
                                .withInt("parentCode", parentCode)
                                .withInt("pageType", 2)
                                .navigation();
                    }
                }, 500);
                break;
            case 3:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ARouter.getInstance()
                                .build(ArouterConstant.ALL_ITEM_ACTIVITY)
                                .withInt("currentId", currentId)
                                .withInt("currentCode", currentCode)
                                .withInt("parentId", parentId)
                                .withInt("parentCode", parentCode)
                                .withInt("pageType", 2)
                                .navigation();
                    }
                }, 500);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    /**
     * 返回上一层
     */
    private void backPage() {
        this.finish();
    }
}
